﻿using System;
using System.Collections.Generic;
using System.Text;
using KKCai.SC.App.DAL;
using KKCai.SC.App.Model;
using Newtonsoft.Json;

namespace KKCai.SC.App
{
    public class Wapper
    {
        public IList<RecordInfo> record;

        public IList<int> count;
    }

    public class Manager
    {
        public string List(int limit)
        {
            Wapper w = new Wapper
            {
                record = new RecordDao().Order("id", false, limit),
                count = new DayCountDao().List()
            };

            return JsonConvert.SerializeObject(w);
        }

        public void Save(RecordInfo info)
        {
            RecordDao dao = new RecordDao();

            RecordInfo r = dao.Order("id", false, 1)[0];

            //int last = Convert.ToInt32(r.TermNo.Substring(6));
            int newid = Convert.ToInt32(info.TermNo.Substring(6));
            //if ((newid == last + 1) || (newid == 1 && last == 84))
            if(r.TermNo != info.TermNo)
            {
                dao.Save(info);

                DayCountDao ddao = new DayCountDao();
                if (newid == 1)
                    ddao.Reset();

                ddao.Update(info.OpenNum);
            }
        }
    }
}
